import { Component } from '@angular/core';
import { IonicPage, LoadingController, NavController, NavParams } from 'ionic-angular';
import { UtilsProvider } from "../../providers/utils/utils";
import { HttpServicesProvider } from "../../providers/http-services/http-services";
import { UrlProvider } from "../../providers/url/url";
import { ConfigProvider } from "../../providers/config/config";
import { ValidateProvider } from "../../providers/validate/validate";
import { StorageProvider } from "../../providers/storage/storage";

@IonicPage({
    segment: 'information',
    defaultHistory: ['HomePage']
})
@Component({
    selector: 'page-information',
    templateUrl: 'information.html',
})
export class InformationPage {
    nav_id: any;
    navIndex: number = 0;
    navlist = [];
    conlist = [];
    page = 1;
    loadObj: any;
    banners = [];
    notices = [];
    searchMethod: any;
    imgs;
    imgArry=[];
    searchData = {
        keyword: {
            value: '',
            text: ''
        }
    };
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public loadingCtrl: LoadingController,
        public util: UtilsProvider,
        public httpService: HttpServicesProvider,
        public url: UrlProvider,
        public config: ConfigProvider,
        public validate: ValidateProvider,
        public storage: StorageProvider
    ) {
    }
    ionViewCanEnter() {
        return this.validate.checkLogin();
    }
    ionViewDidLoad() {
        this.validate.usualShare();
    }
    ionViewWillEnter() {
        if (this.navParams.get('nav_id')) {
            this.nav_id = this.navParams.get('nav_id');
            this.searchMethod = this.navParams.get('searchMethod');
        } else {
            this.nav_id = 3;
            this.searchMethod = 'normal';
        }
        this.navIndex = this.navParams.get('index');
        this.getAds();
        this.getNavs();
        this.getData(null);
    }

    /**
     * 获取广告
     * */
    getAds() {
        this.httpService.requestData('/service/get_ads/type_id/' + this.nav_id, response => {
            //全部数据
            let data = response['data'];
            this.banners = data['image_list'];
            this.notices = data['text_list'];
        });

    }


    /**
     * 获取顶部分类导航
     * */
    getNavs() {
        this.httpService.requestData('/task_info/information_nav', response => {
            if (response['error'] == 200) {
                this.navlist = response['data'];
            }
        });
    }
    /**
     * 获取页面数据
     * */
    getData(infiniteScroll) {
        let loading = this.util.loading();
        loading.show();
        let postData = {
            taskinfo: 2,
            type_id: this.nav_id,
            page: this.page,
            searchMethod: this.searchMethod, //常规搜索方式和高级搜索方式
        };

        Object.assign(postData, { keyword: this.searchData['keyword']['value'] });
        if (this.storage.get('resumeSearchData')) {
            // Object.assign(postData,JSON.parse(this.storage.get('resumeSearchData')));
            let searchKeysArr = [];
            let searchKeys = JSON.parse(this.storage.get('resumeSearchData'));
            for (let key in searchKeys) {
                if (key == 'time') {
                    Object.assign(postData, { time: searchKeys[key] });
                    continue;
                }
                if (key == 'credit_line') {
                    Object.assign(postData, { credit_line: searchKeys[key] });
                    continue;
                }
                if (key == 'sex') {
                    searchKeys[key] = searchKeys[key] == 1 ? '男' : '女';
                }
                searchKeysArr.push(searchKeys[key]);
            }
            Object.assign(postData, { content: JSON.stringify(searchKeysArr) });
        }

        let listUrl = '/task_info/index_option';

        this.httpService.requestPostData(listUrl, postData, response => {
            loading.close();
            
            if (response.code == 200) {
                //全部数据
                let data = response['data'];
                this.conlist = this.conlist.concat(data['data']);
                this.page++;
                if (infiniteScroll) {                          
                    infiniteScroll.complete();
                    /*请求完成数据更新页面*/
                    if (this.conlist.length>data.total) {   /*没有数据 禁用上拉更新*/
                        infiniteScroll.enable(false);
                    }
                } else {
                    //顶部子分类列表
                    this.navlist = response['navlist'];
                    //内容数据
                    // this.conlist = data['data'];
                }
            } else {
                if (infiniteScroll) {
                    infiniteScroll.enable(false);
                }
            }
        });
    }

    doInfinite(infiniteScroll) {   
        this.getData(infiniteScroll);
    }

    goPage(page, data = {}) {
        this.navCtrl.push(page, data).then(success => {
            if (success !== true) {//页面需要登录后跳转
                this.navCtrl.push('LoginPage', data);
            }
        })
    }
    //禁止冒泡
    alert(e){
        e.stopPropagation()
    }
    bigPhoto(imgArry,index){
        let photoArry=[];
        for(let k =0;k<imgArry.length;k++){
            photoArry.push(imgArry[k].content)
        }
        this.util.imageSlide(photoArry, index)
    }
    /**
     * 切换分类..
     * 加载新页面后删除上一个页面
     * */
    changeNav(nav, index = 0) {
        if (nav.type_id == this.nav_id) {
            return false;
        } else {
            let currPage = this.navCtrl.getActive();
            this.navCtrl.push('InformationPage', {
                nav_id: nav.type_id,
                index
            });
            this.navCtrl.removeView(currPage);
        }
    }

    //跳转页面
    pageRedirect(page, data = {}) {
        this.navCtrl.push(page, data).then(success => {
            if (success !== true) {//页面需要登录后跳转
                this.navCtrl.push('LoginPage', data);
            }
        })
    }

    /**
     * 离开页面时删除简历搜索项
     * */
    ionViewWillLeave() {
        if (this.storage.get('resumeSearchData')) {
            this.storage.remove('resumeSearchData');
        }
    }

    search() {
        this.page = 1;
        this.conlist = [];
        this.getData(null);
    }

}
